﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vw08_equest.Areas.Administrator.Models;

namespace vw08_equest.Areas.Administrator.Controllers
{
    public class AdminExamController : Controller
    {
        //
        // GET: /Administrator/AdminExam/

        public ActionResult Index(string message = "", string level = "")
        {
            if (level == "Success")
            {
                ViewBag.Success = message;
            }
            if (level == "Warning")
            {
                ViewBag.Warning = message;
            }
            if (level == "Error")
            {
                ViewBag.Error = message;
            }
            using (AdminEquestDataContext db = new AdminEquestDataContext())
            {
                List<Exam> model = new List<Exam>();
                model = db.Exams.Where(m => m.IsDelete == false).OrderByDescending(m => m.CreatedDate).ToList();
                return View(model);
            }
        }

        public ActionResult CreateExam()
        {
            using (AdminEquestDataContext db = new AdminEquestDataContext())
            {
                Exam model = new Exam();
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult CreateExam(Exam model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.Warning = "Dữ liệu đợt thi không đúng!";
                    return View(model);
                }
                using (AdminEquestDataContext db = new AdminEquestDataContext())
                {
                    model.Default();
                    db.GetTable(typeof(Exam)).InsertOnSubmit(model);
                    db.SubmitChanges();
                    return RedirectToAction("Index", new { message = "Thêm đợt thi thành công!", level = "Success" });
                }
            }
            catch (Exception)
            {
                ViewBag.Error = "Lỗi tạo đợt thi!";
                return View(model);
            }
        }

        public ActionResult EditExam(int id)
        {
            using (AdminEquestDataContext db = new AdminEquestDataContext())
            {
                Exam model = db.Exams.Where(m => m.ExamID == id).SingleOrDefault();
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult EditExam(Exam model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.Warning = "Dữ liệu đợt thi không đúng!";
                    return View(model);
                }
                using (AdminEquestDataContext db = new AdminEquestDataContext())
                {
                    Exam updateModel = db.Exams.Where(m => m.ExamID == model.ExamID).SingleOrDefault();
                    updateModel.ExamName = model.ExamName;
                    updateModel.MaxPoint = model.MaxPoint;
                    updateModel.MaxMinute = model.MaxMinute;
                    updateModel.HardQuestion = model.HardQuestion;
                    updateModel.MediumQuestion = model.MediumQuestion;
                    updateModel.EasyQuestion = model.EasyQuestion;
                    updateModel.IsInUse = model.IsInUse;
                    db.SubmitChanges();
                    ViewBag.Success = "Cập nhật đợt thi thành công!";
                    return View(updateModel);
                }
            }
            catch (Exception)
            {
                ViewBag.Error = "Lỗi cập nhật đợt thi!";
                return View(model);
            }
        }

        [HttpPost]
        public ActionResult DeleteExam(FormCollection form)
        {
            try
            {
                int ExamID = Convert.ToInt32(form["id"]);
                using (AdminEquestDataContext db = new AdminEquestDataContext())
                {
                    Exam model = db.Exams.Where(m => m.ExamID == ExamID).SingleOrDefault();
                    model.IsDelete = true;
                    db.SubmitChanges();
                    return RedirectToAction("Index", new { message = "Xoá đợt thi thành công!", level = "Success" });
                }
            }
            catch (Exception)
            {
                return RedirectToAction("Index", new { message = "Lỗi xóa đợt thi!", level = "Error" });
            }
        }
    }
}
